/**
 * Created by doucharles1 on 16/4/9.
 */
var util=require("util")
var async=require("async")
var _=require("underscore")
var ObjectID=require("mongodb").ObjectID
$load("MyUtil.js")

var arrRoutes=[
    ["post","","$form",submitDonation],
    ["put",":id_donation","$form","$params",updateDonation],
    ["post","usage/:_id_donation","$form","$params",addDonationUsage],
    ["put","usage/:_id_donation_usage","$form","$params",updateDonationUsage],
    ["get","detail/:id",queryOneDetail],
    ["get","donations/:page/:perpage","$filter",syncDonations],
    ["get","usages/:_id_donation/:page/:perpage","$filter",syncDonationUsages],
    ["get","users/:_id_donation/:page/perpage","$filter",syncDonationUsers]
]

function DonationController(arrRoutes,strRoutePrefix,strViewPrefix,strSubApp){
    Controller.call(this,arrRoutes,strRoutePrefix,strViewPrefix,strSubApp)
}

util.inherits(DonationController,Controller)

function submitDonation(req,res,next){
    $dao["donation"]["insertOneDonation"](req.form,_.bind(res.reply,res))
}

function updateDonation(req,res,next){
    var objDonationID=new ObjectID(req.params.id)
    $dao["donation"]["updateOneDonation"](objDonationID,req.form,_.bind(res.reply,res))
}

function updateDonationUsage(req,res,next){
    $dao["donation"]["updateDonationUsage"](req.params["_id_donation"],req.ocid,req.body,_.bind(res.reply,res))
}

function queryOneDetail(req,res,next){
    $dao["donation"]["detailOneDonation"](new ObjectID(req.params.id),_.bind(res.reply,res))
}

function addDonationUsage(req,res,next){
    $dao["donation"]["insertDonationUsage"](new ObjectID(req.params.id),req.form,_.bind(res.reply,res))
}

function syncDonations(req,res,next){
    $dao["donation"]["syncDonations"](req.ocid,req.params["page"],req.params["perpage"],req.filter,req.sort,_.bind(res.reply,res))
}

function syncDonationUsages(req,res,next){
    $dao["donation"]["syncDonationUsages"](req.params["_id_donation"],req.ocid,req.params["page"],req.params["perpage"],req.filter,req.sort,_.bind(res.reply,res))
}

function syncDonationUsers(req,res,next){
    $dao["donation"]["syncDonationUsers"](req.params["_id_donation"],req.params["page"],rea.params["perpage"],req.filter,req.sort,_.bind(res.reply,res))
}

module.exports=new DonationController(arrRoutes,"donation","donation")
